import { defineStore } from "pinia";
import { ref } from "vue";

type UserInfo = {
  id: string;
  account: string;
  nickname: string;
}

const useUserStore = defineStore("token", () => {
  const token = ref<string>();

  const loggedIn = ref(false)

  const userInfo = ref<UserInfo>()

  const login = (value: string) => {
    token.value = value;
    loggedIn.value = true
    localStorage.setItem('token', value)
  };

  const logout = () => {
    token.value = undefined;
    loggedIn.value = false
    userInfo.value = undefined
    localStorage.removeItem('token')
  }

  const setUserInfo = (latestUserInfo: UserInfo) => {
    userInfo.value = latestUserInfo
  }

  return { userInfo, loggedIn, token, login, logout, setUserInfo };
});

export default useUserStore;
